<?php

App::uses("AttachAbstract", "Lib/attach");
// App::uses('File', 'Utility');
// App::uses('Folder', 'Utility');
App::uses('StringUtil', 'Lib');


/**
 * 
 */
class AttachFILE extends AttachAbstract {
	
	function __construct(&$attach) {
		$this->attach=$attach;
	}
	
	public function save_old() {
		try {
			if($this->attach->attachTipo->data['AttachTipo']['path_assoluto']=='N') {
				$success = move_uploaded_file($this->attach->getFileTemporaneo(), 
						$this->attach->attachTipo->data['AttachTipo']['path']."/".$this->attach->id."_".$this->attach->field('file_name'));
				if (!$success) {
					throw new Exception("Errore in fase di copia dell'attach");
				}
			} else {
				$success = move_uploaded_file($this->attach->getFileTemporaneo(), $this->attach->attachTipo->data['AttachTipo']['path']."/".$this->attach->id."_".$this->attach->field('file_name'));
				if (!$success) {
					throw new Exception("Errore in fase di copia dell'attach");
				}
			}	
		} catch (exception $e) {
			throw new Exception("Errore in fase di copia dell'attach");
		}
	}
	
	public function save() {
		try {
			
			if($this->attach->attachTipo->data['AttachTipo']['path_assoluto']=='N') {
				// print_r(WWW_ROOT);die;
				$estensione = StringUtil::getExt($this->attach->field('file_name'));
				$dest = WWW_ROOT.$this->attach->attachTipo->data['AttachTipo']['path'].$this->attach->field('path');
				if  (!file_exists($dest)) {
					mkdir($dest, 0777);
				}				
				$filename = $this->attach->id.".".$estensione;
				$success = move_uploaded_file($this->attach->getFileTemporaneo(), $dest."/".$filename);
				if (!$success) {
					throw new Exception("Errore in fase di copia dell'attach");
				}
				chmod($dest."/".$filename, 0777);
			} else {
				$estensione = StringUtil::getExt($this->attach->field('file_name'));
				$dest = $this->attach->attachTipo->data['AttachTipo']['path'].$this->attach->field('path');
				if  (!file_exists($dest)) {
					mkdir($dest, 0777);
				}				
				$filename = $this->attach->id.".".$estensione;
				$success = move_uploaded_file($this->attach->getFileTemporaneo(), $dest."/".$filename);
				if (!$success) {
					throw new Exception("Errore in fase di copia dell'attach");
				}
				chmod($dest."/".$filename, 0777);
			}
			$this->attach->path = $dest;
			$this->attach->filename = $filename;
			return $dest."/".$filename;
		} catch (exception $e) {
			throw new Exception("Errore in fase di copia dell'attach");
		}
	}
	
	
	
	
	public function read() {
		
		try {
			if($this->attach->attachTipo->data['AttachTipo']['path_assoluto']=='N') {
				// print_r(WWW_ROOT);die;
				$estensione = StringUtil::getExt($this->attach->field('file_name'));
				// $dest = WWW_ROOT.$this->attach->attachTipo->data['AttachTipo']['path'].$this->attach->field('path');
				$dest = $this->attach->attachTipo->data['AttachTipo']['path'].$this->attach->field('path');
				$filename = $this->attach->id.".".$estensione;
				if  (!file_exists($dest."/".$filename)) {
					throw new Exception("File inesistente.", 1);
				} else {
					// print_r($dest."/".$filename);die;
					$this->attach->path = $dest;
					$this->attach->filename = $filename;
					return $dest."/".$filename;
				}
			} else {
				$estensione = StringUtil::getExt($this->attach->field('file_name'));
				$dest = $this->attach->attachTipo->data['AttachTipo']['path'].$this->attach->field('path');
				$filename = $this->attach->id.".".$estensione;
				if  (!file_exists($dest."/".$filename)) {
					throw new Exception("File inesistente.", 1);
				} else {
					$this->attach->path = $dest;
					$this->attach->filename = $filename;
					return $dest."/".$filename;
				}
			}	
		} catch (exception $e) {
			throw new Exception("Errore in fase di lettura dell'attach");
		}
		
	}

	public function delete() {
		try {
			if($this->attach->attachTipo->data['AttachTipo']['path_assoluto']=='N') {
				// print_r(WWW_ROOT);die;
				$estensione = StringUtil::getExt($this->attach->field('file_name'));
				$dest = WWW_ROOT.$this->attach->attachTipo->data['AttachTipo']['path'].$this->attach->field('path');
				$filename = $this->attach->id.".".$estensione;
				$success = unlink($dest."/".$filename);
				if (!$success) {
					throw new Exception("Errore in fase di cancellazione dell'attach");
				}
			} else {
				$estensione = StringUtil::getExt($this->attach->field('file_name'));
				$dest = $this->attach->attachTipo->data['AttachTipo']['path'].$this->attach->field('path');
				$filename = $this->attach->id.".".$estensione;
				$success = unlink($dest."/".$filename);
				if (!$success) {
					throw new Exception("Errore in fase di cancellazione dell'attach");
				}
			}	
		} catch (exception $e) {
			throw new Exception("Errore in fase di cancellazione dell'attach");
		}
	}
	
}



?>